package com.dream.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.RecommendMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.Recommend;
import com.dream.pojo.RecommendExample;
import com.dream.pojo.RecommendSon;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Recommend_able;

/*
 * 作者推荐表的业务
 */
@Service("Recommend_Serviceimpl")
public class Recommend_Serviceimpl 
implements All_Service_able<Recommend>,
Recommend_able<RecommendSon>{

	/*
	 * 推荐表的DAO代理对象！
	 */
	@Resource(name="recommendMapper")
	private RecommendMapper recommend_mapper;
	
	/*
	 * (添加一条推荐，传入文章id)
	 * @see com.dream.service.able.All_Service_able#insert(java.lang.String[])
	 */
	@Override
	public Recommend insert(String... str) {
		//切换数据库，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建推荐文章表的实例
		Recommend recommend = new Recommend();
		//设置文章id
		recommend.setAid(Integer.parseInt(str[0]));
		//设置状态
		recommend.setState(1);
		//写入数据库
		return recommend_mapper.insertSelective(recommend)>0?recommend:null;
	}

	@Override
	public List<Recommend> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Recommend getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (查询作者推荐)
	 * @see com.dream.service.able.Recommend_able#ListRecommend(java.lang.String[])
	 */
	@Override
	public List<RecommendSon> ListRecommend(String... str) {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		RecommendExample example = new RecommendExample();
		//设置排序规则
		example.setOrderByClause("rid desc");
		//设置查询开始位置
		example.setOffset(Integer.parseInt(str[0]));
		//设置返回条数
		example.setLimit(Integer.parseInt(str[1]));
		//创建查询条件工具内部类
		RecommendExample.Criteria criteria = example.createCriteria();
		//设置查询条件,state=1
		criteria.andStateEqualTo(1);
		//返回查询到的结果集
		return recommend_mapper.listRecommend(example);
	}

}
